package com.wangdao.wechatmall.service.admin;

import com.github.pagehelper.PageHelper;
import com.wangdao.wechatmall.bean.bo.admin.KeyWordBO;
import com.wangdao.wechatmall.bean.bo.admin.KeyWordParamBO;
import com.wangdao.wechatmall.bean.pojo.Keyword;
import com.wangdao.wechatmall.bean.pojo.KeywordExample;
import com.wangdao.wechatmall.bean.vo.admin.KeyWordRespVO;
import com.wangdao.wechatmall.bean.vo.admin.KeyWordVO;
import com.wangdao.wechatmall.mapper.KeywordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * KeyWordServiceImpl
 * created by feng on 7/9/21
 */

@Service
public class KeyWordServiceImpl implements KeyWordService {
    @Autowired
    KeywordMapper keywordMapper;

    @Override
    public KeyWordVO getKeyWord(KeyWordBO keyWordBO) {
        PageHelper.startPage(keyWordBO.getPage(), keyWordBO.getLimit());

        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();

        // 未删除
        criteria.andDeletedEqualTo(0);

        // keyword
        if (keyWordBO.getKeyword() != null && !"".equals(keyWordBO.getKeyword())) {
            criteria.andKeywordLike("%" + keyWordBO.getKeyword() + "%");
        }

        // url
        if (keyWordBO.getUrl() != null && !"".equals(keyWordBO.getUrl())) {
            criteria.andUrlLike("%" + keyWordBO.getUrl() + "%");
        }

        // 分页
        keywordExample.setOrderByClause(keyWordBO.getSort() + " " + keyWordBO.getOrder());


        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);

        KeyWordVO keyWordVO = new KeyWordVO();
        int size = keywords.size();
        keyWordVO.setTotal(size);
        keyWordVO.setItems(keywords);

        return keyWordVO;
    }

    @Override
    public Integer delete(Keyword keyword) {

        // Keyword example
        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andIdEqualTo(keyword.getId());

        // keyword record
        Keyword keywordToChange = new Keyword();
        keywordToChange.setDeleted(true);

        int affectRow = keywordMapper.updateByExampleSelective(keywordToChange, keywordExample);

        return affectRow;
    }

    @Override
    public KeyWordRespVO create(KeyWordParamBO keywordParamBO) {
        //Date
        Date date = new Date();

        //keyword
        Keyword keyword = new Keyword();
        keyword.setKeyword(keywordParamBO.getKeyword());
        keyword.setUrl(keywordParamBO.getUrl());
        keyword.setIsHot(keywordParamBO.getIsHot());
        keyword.setIsDefault(keywordParamBO.getIsDefault());
        keyword.setAddTime(date);
        keyword.setUpdateTime(date);
        keyword.setDeleted(false);

        keywordMapper.insertSelective(keyword);
        System.out.println("刚才插入的id是" + keyword.getId());

        //Return KeyWordRespVO
        KeyWordRespVO keyWordRespVO = new KeyWordRespVO();
        Keyword getKeyword = keywordMapper.selectByPrimaryKey(keyword.getId());

        keyWordRespVO.setId(getKeyword.getId());
        keyWordRespVO.setKeyword(getKeyword.getKeyword());
        keyWordRespVO.setAddTime(getKeyword.getAddTime());
        keyWordRespVO.setUpdateTime(getKeyword.getUpdateTime());
        keyWordRespVO.setIsDefault(getKeyword.getIsDefault());
        keyWordRespVO.setIsHot(getKeyword.getIsHot());
        keyWordRespVO.setUrl(getKeyword.getUrl());

        return keyWordRespVO;
    }

    @Override
    public Keyword update(Keyword keyword) {
        // update
        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andIdEqualTo(keyword.getId());

        // update UpdateTime
        Date date = new Date();
        keyword.setUpdateTime(date);

        keywordMapper.updateByExampleSelective(keyword, keywordExample);

        // return data
        Keyword returnKeyword = keywordMapper.selectByPrimaryKey(keyword.getId());

        return returnKeyword;
    }
}